package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.service.OmsDistributionOrderService;
import com.macro.mall.portal.service.UmsMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@Controller
@RequestMapping("/commission")
public class CommissionWithdrawalController {
    @Autowired
    private OmsDistributionOrderService omsDistributionOrderService;
    @Autowired
    private UmsMemberService memberService;

    /**
     * 佣金订单列表
     *
     * @param pageSize
     * @param pageNum
     * @return
     * @author zhouboyu
     * @Date 2020年9月15日
     */
    @ApiOperation("佣金订单列表")
    @RequestMapping(value = "/distributionOrderList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> distributionOrderList(
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) String start, @RequestParam(required = false) String end) {
        Date startDate = null;
        Date endDate = null;
        try {
            if(start == null || "".equals(start)){
                startDate = null;
            }else{
                startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(start+" 00:00:00");
            }
            if(start == null || "".equals(start)){
                endDate = null;
            }else{
                endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(end+" 23:59:59");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return CommonResult.success(this.omsDistributionOrderService.getList(pageNum, pageSize, startDate, endDate));
    }

    @ApiOperation("佣金合计")
    @RequestMapping(value = "/getAllCommission", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BigDecimal> getAllCommission(
            @RequestParam(required = false) String start, @RequestParam(required = false) String end) {
        Date startDate = null;
        Date endDate = null;
        try {
            if(start == null || "".equals(start)){
                startDate = null;
            }else{
                startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(start+" 00:00:00");
            }
            if(start == null || "".equals(start)){
                endDate = null;
            }else{
                endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(end+" 23:59:59");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return CommonResult.success(this.omsDistributionOrderService.getAllCommission(startDate, endDate));
    }

    /**
     * 本月业绩总额
     *
     * @return
     * @author zhouboyu
     * @Date 2020年9月15日
     */
    @ApiOperation("本月业绩总额")
    @RequestMapping(value = "/totalAmount", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BigDecimal> totalAmount(
            @RequestParam(required = false) String start, @RequestParam(required = false) String end) {
        UmsMember currentMember = this.memberService.getCurrentMember();
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(start+" 00:00:00");
            endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(end+" 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return CommonResult.success(this.omsDistributionOrderService.getTotalAmount(currentMember.getId(), startDate, endDate));
    }

    /**
     * 提现金额，可提现，不可提现
     *
     * @return
     * @author zhouboyu
     * @Date 2020年9月15日
     */
    @ApiOperation("提现金额，可提现，不可提现")
    @RequestMapping(value = "/withdrawal/amount", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, BigDecimal>> withdrawalAmount() {
        return CommonResult.success(this.omsDistributionOrderService.getWithdrawalAmount());
    }

    /**
     * 提现记录
     *
     * @return
     * @author zhouboyu
     * @Date 2020年9月15日
     */
    @ApiOperation("提现记录")
    @RequestMapping(value = "/withdrawal/record", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> withdrawalRecord(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                              @RequestParam(required = false) String start) {
        //@RequestParam(required = false)  Date end
        Date startDate = null;
        try {
            start = start + "-01 00:00:00";
            startDate = new SimpleDateFormat("yyyy-MM").parse(start);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return CommonResult.success(this.omsDistributionOrderService.withdrawalRecord(pageNum, pageSize, startDate));
    }

    /**
     * 申请提现
     *
     * @return
     * @author zhouboyu
     * @Date 2020年9月16日
     */
    @ApiOperation("申请提现")
    @RequestMapping(value = "/withdrawal/apply", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult withdrawalApply(@RequestParam(value = "amount", defaultValue = "1") BigDecimal amount) {
        return this.omsDistributionOrderService.withdrawalApply(amount);
    }
}
